#include <fstream>
#include <iostream>
#include <vector>

namespace part2
{

class RaceData
{
public:
    RaceData(std::int64_t t, std::int64_t d) : 
        _time{t}, _distance{d} {}

    std::size_t getBeatNumber() const 
    {
        // distance = (time - hold_time) * hold_time 
        // ==> time * X - X^2
        // max value : X = time / 2
        // from 0 => time, find the first value satisfy condition
        std::size_t number {0};
        for (std::int64_t holdTime = 0; holdTime <= this->_time; ++holdTime)
        {
            std::int64_t travelTime {this->_time - holdTime};
            std::int64_t speed {holdTime};
            if (travelTime * speed >= this->_distance)
                number += 1;
        }
        return number;
    }

private:
    std::int64_t _time;
    std::int64_t _distance;

public:
    std::int64_t time() const noexcept
    { return this->_time; }
    
    std::int64_t distance() const noexcept
    { return this->_distance; }
};

std::int64_t getNumber(std::string& line)
{
    auto isdigit = [] (char ch) -> bool { return ch >= '0' && ch <= '9'; };
    std::string digit {};
    for (char ch : line)
    {
        if (isdigit(ch))
            digit.push_back(ch);
    }
    std::cout << digit << std::endl;
    return std::stoll(digit);
}

RaceData getData(std::ifstream& file)
{
    std::string line {};
    std::getline(file, line);
    std::int64_t time {getNumber(line)};

    std::getline(file, line);
    std::int64_t distance {getNumber(line)};

    return RaceData{time, distance};
}

void solute()
{
    std::ifstream file {"./src/day06/input.txt"};
    RaceData data {getData(file)};
    std::cout << data.getBeatNumber() << std::endl;
}

}